﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace CPPEI.PM.Shell
{
    /// <summary>
    /// Interaction logic for SelectProjectWindow.xaml
    /// </summary>
    public partial class SelectProjectWindow : Window
    {
        public SelectProjectWindow()
        {
            InitializeComponent();

            PMServiceRef.PMWebServiceSoapClient client = new PMServiceRef.PMWebServiceSoapClient();
            PMServiceRef.ListResultOfProject result = client.GetProjectList(UIContext.Instance.Account);
            this._ctlListProject.ItemsSource = result.Datas;
        }

        private void _ctlBtnSelect_Click_1(object sender, RoutedEventArgs e)
        {
            PMServiceRef.Project project = this._ctlListProject.SelectedItem as PMServiceRef.Project;
            if (project != null)
            {
                UIContext.Instance.ProjectId = project.Id;

                Project localProject = new PM.PMService().LoadProject(project.Id);
                if (localProject != null)
                {
                    if(localProject.Changeset > 0)
                    {
                        // 使用本地数据
                        UIContext.Instance.Project = new PM.PMService().LoadFullProject(project.Id);
                    }
                    else
                    {
                        // 使用服务器数据
                        UIContext.Instance.Project = new Project();
                        UIContext.Instance.Project.Id = project.Id;
                        UIContext.Instance.Project.Caption = project.Caption;
                    }
                }
                else
                {
                    // 创建项目信息对象
                    UIContext.Instance.Project = new Project();
                    UIContext.Instance.Project.Id = project.Id;
                    UIContext.Instance.Project.Caption = project.Caption;
                }

                DialogResult = true;
            }
        }

        private void _ctlBtnCreate_Click_1(object sender, RoutedEventArgs e)
        {
            PMServiceRef.PMWebServiceSoapClient client = new PMServiceRef.PMWebServiceSoapClient();
            client.CreateProject(UIContext.Instance.Account, this._ctlEditCaption.Text);

            PMServiceRef.ListResultOfProject result = client.GetProjectList(UIContext.Instance.Account);
            this._ctlListProject.ItemsSource = result.Datas;
        }
    }
}
